/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.senac.controle;

import br.com.ecommercer.bean.Endereco;
import br.com.ecommercer.dao.EnderecoDao;
import br.com.ecommercer.dao.EnderecoDaoImp;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * *
 * @author Petterson
 */
@WebServlet(name = "EnderecoServlet", urlPatterns = {"/EnderecoServlet"})
public class EnderecoServlet extends HttpServlet {

    private HttpServletRequest request;
    private HttpServletResponse response;
    private RequestDispatcher rd;
    private Endereco endereco;
    private EnderecoDao enderecoDao;

    protected void processRequest() throws ServletException, IOException {
        String cmd = request.getParameter("cmd");
        switch (cmd) {
            case "salvar":
                salvar();
                break;
            case "alterar":
                alterar();
                break;
            case "excluir":
                excluir();
                break;
            case "procurarId":
                procurarPorId();
                break;
            case "pesqLocal":
                procuraPorLocal();
                break;
        }
        rd.forward(request, response);
    }

    private void carregaEndereco() {
        endereco = new Endereco();
        String id = request.getParameter("id");
        if (id != null) {
            endereco.setId(Integer.parseInt(id));
        }
        endereco.setLocal(request.getParameter("local"));
        endereco.setRua(request.getParameter("rua"));
        endereco.setBairro(request.getParameter("bairro"));
        endereco.setCidade(request.getParameter("cidade"));
        endereco.setEstado(request.getParameter("estado"));
        endereco.setCep(request.getParameter("cep"));
        endereco.setCapacidadeMaxima(Integer.parseInt(request.getParameter("capacidadeMaxima")));

    }

    private void salvar() {
        carregaEndereco();
        enderecoDao = new EnderecoDaoImp();
        try {
            enderecoDao.salvar(endereco);
            request.setAttribute("msg", "endereço salvo com sucesso!");
        } catch (Exception e) {
            request.setAttribute("msg", "erro ao salvar Endereço!");
        }
        rd = request.getRequestDispatcher("index.jsp");
    }

    private void procuraPorLocal() {
        endereco = new Endereco();
        enderecoDao = new EnderecoDaoImp();
        try {
            List enderecos = enderecoDao.procurarLocal(request.getParameter("local"));
            if (enderecos.isEmpty()) {
                request.setAttribute("msg", "Não existe local(s) salvo(s)");
            }
            request.setAttribute("enderecos", enderecos);
            rd = request.getRequestDispatcher("pesq_endereco.jsp");
        } catch (Exception e) {
            request.setAttribute("msg", "Erro ao procurar local(s)");
        }
    }

    private void procurarPorId() {
        enderecoDao = new EnderecoDaoImp();
        Integer id = Integer.parseInt(request.getParameter("id"));
        try {
            endereco = (Endereco) enderecoDao.procurar(id);
            request.setAttribute("endereco", endereco);
            rd = request.getRequestDispatcher("alt_endereco.jsp");
        } catch (Exception e) {
        }
    }

    private void alterar() {
        enderecoDao = new EnderecoDaoImp();
        carregaEndereco();
        try {
            enderecoDao.alterar(endereco);
            request.setAttribute("msg", "endereco alterado com sucesso!");
            rd = request.getRequestDispatcher("pesq_endereco.jsp");
        } catch (Exception e) {
            request.setAttribute("msg", "erro ao alterar endereco!");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.request = request;
        this.response = response;
        processRequest();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.request = request;
        this.response = response;
        processRequest();
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void excluir() {
        Integer id = Integer.parseInt(request.getParameter("id"));
        try {
            enderecoDao.excluir(id);
            request.setAttribute("msg", "excluido com sucesso!");
        } catch (Exception e) {
            request.setAttribute("msg", "erro ao excluir!");
        }
        rd = request.getRequestDispatcher("pesq_endereco.jsp");
    }
}
